﻿using AMAP.Domain;
using AMAP.ServiceClasses.Common;
using AMAP.ServiceInterfaces;
using AMAP.ServiceInterfaces.Common;
using System;
using System.Collections.Generic;
using System.Linq;

namespace AMAP.ServiceClasses
{
    public class BasketOrderService : BaseRepositoryService<BasketOrder>, IBasketOrderService
    {
        public BasketOrderService(IDatabaseFactory databaseFactory)
            : base(databaseFactory)
        {
        }

        public BasketOrder GetLast(int ClientId)
        {
            return this.dataContext.BasketOrderSet.Where(b => b.ClientId == ClientId).OrderByDescending(b => b.Id).FirstOrDefault();
        }

        public IList<BasketOrder> GetCurrentWeekList()
        {
            DateTime Week = DateTime.Now;
            int CurrentDayOfWeek = (int)Week.DayOfWeek - 1;
            if (CurrentDayOfWeek < 0)
                CurrentDayOfWeek = 6;
            DateTime LowLimit = Week.AddDays(-1 * CurrentDayOfWeek);
            LowLimit = DateTime.Parse(LowLimit.ToShortDateString());
            DateTime HighLimit = Week.AddDays(7 - CurrentDayOfWeek);
            LowLimit = DateTime.Parse(HighLimit.ToShortDateString());

            return this.dataContext.BasketOrderSet.Where(b => b.CreationDate >= LowLimit && b.CreationDate < HighLimit).ToList<BasketOrder>();
        }
    }
}
